1
|
|
|
var Subject = require('./Subject'), |
2
|
|
|
GDate = require('./Date'), |
3
|
|
|
PlaceReference = require('./PlaceReference'), |
4
|
|
|
EventRole = require('./EventRole'), |
5
|
|
|
utils = require('./utils'); |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An event. |
9
|
|
|
* |
10
|
|
|
* @constructor |
11
|
|
|
* @param {Object} [json] |
|
|
|
|
12
|
|
|
*/ |
13
|
|
|
var Event = function(json){ |
14
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
16
|
|
|
if(!(this instanceof Event)){ |
17
|
|
|
return new Event(json); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
21
|
|
|
if(Event.isInstance(json)){ |
22
|
|
|
return json; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
Subject.call(this, json); |
26
|
|
|
|
27
|
|
|
if(json){ |
|
|
|
|
28
|
|
|
this.setType(json.type); |
29
|
|
|
this.setDate(json.date); |
30
|
|
|
this.setPlace(json.place); |
31
|
|
|
this.setRoles(json.roles); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
Event.prototype = Object.create(Subject.prototype); |
36
|
|
|
|
37
|
|
|
Event._gedxClass = Event.prototype._gedxClass = 'GedcomX.Event'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check whether the given object is an instance of this class. |
41
|
|
|
* |
42
|
|
|
* @param {Object} obj |
43
|
|
|
* @returns {Boolean} |
44
|
|
|
*/ |
45
|
|
|
Event.isInstance = function(obj){ |
46
|
|
|
return utils.isInstance(obj, this._gedxClass); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the type |
51
|
|
|
* |
52
|
|
|
* @returns {String} |
53
|
|
|
*/ |
54
|
|
|
Event.prototype.getType = function(){ |
55
|
|
|
return this.type; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the type |
60
|
|
|
* |
61
|
|
|
* @param {String} type |
62
|
|
|
* @returns {Event} |
63
|
|
|
*/ |
64
|
|
|
Event.prototype.setType = function(type){ |
65
|
|
|
this.type = type; |
66
|
|
|
return this; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the date |
71
|
|
|
* |
72
|
|
|
* @returns {Date} |
73
|
|
|
*/ |
74
|
|
|
Event.prototype.getDate = function(){ |
75
|
|
|
return this.date; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the date |
80
|
|
|
* |
81
|
|
|
* @param {Date} date |
82
|
|
|
* @returns {Event} |
83
|
|
|
*/ |
84
|
|
|
Event.prototype.setDate = function(date){ |
85
|
|
|
if(date){ |
86
|
|
|
this.date = GDate(date); |
87
|
|
|
} |
88
|
|
|
return this; |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the place |
93
|
|
|
* |
94
|
|
|
* @returns {PlaceReference} |
95
|
|
|
*/ |
96
|
|
|
Event.prototype.getPlace = function(){ |
97
|
|
|
return this.place; |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the place |
102
|
|
|
* |
103
|
|
|
* @param {PlaceReference} place |
104
|
|
|
* @returns {Event} |
105
|
|
|
*/ |
106
|
|
|
Event.prototype.setPlace = function(place){ |
107
|
|
|
if(place){ |
108
|
|
|
this.place = PlaceReference(place); |
109
|
|
|
} |
110
|
|
|
return this; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the roles |
115
|
|
|
* |
116
|
|
|
* @returns {EventRole[]} |
117
|
|
|
*/ |
118
|
|
|
Event.prototype.getRoles = function(){ |
119
|
|
|
return this.roles || []; |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the roles |
124
|
|
|
* |
125
|
|
|
* @param {EventRole[]|Object[]} roles |
126
|
|
|
* @returns {Event} |
127
|
|
|
*/ |
128
|
|
|
Event.prototype.setRoles = function(roles){ |
129
|
|
|
return this._setArray(roles, 'roles', 'addRole'); |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add a role |
134
|
|
|
* |
135
|
|
|
* @param {EventRole|Object} role |
136
|
|
|
* @returns {Event} |
137
|
|
|
*/ |
138
|
|
|
Event.prototype.addRole = function(role){ |
139
|
|
|
return this._arrayPush(role, 'roles', EventRole); |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Export the object as JSON |
144
|
|
|
* |
145
|
|
|
* @return {Object} JSON object |
146
|
|
|
*/ |
147
|
|
|
Event.prototype.toJSON = function(){ |
148
|
|
|
return this._toJSON(Subject, [ |
149
|
|
|
'type', |
150
|
|
|
'date', |
151
|
|
|
'place', |
152
|
|
|
'roles' |
153
|
|
|
]); |
154
|
|
|
}; |
155
|
|
|
|
156
|
|
|
module.exports = Event; |